package com.haoyu.microservicecloudconsumercategory80.service;

import com.haoyu.microservicecloudapi.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 蒿雨
 * @create: 2022-04-14 10:17
 * @description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    LoadBalancerClient loadBalancerClient;

    private static final String categoruUrl = "http://microservicecloud-provider-category";

    @Override
    public List<Category> list() {
        System.out.println(loadBalancerClient.choose("microservicecloud-provider-category").getPort());
        return restTemplate.getForObject(categoruUrl+"/category", List.class);
    }

    @Override
    public Category get(Integer id) {
        return restTemplate.getForObject(categoruUrl + "/" + id + "/category", Category.class);
    }

    @Override
    public boolean update(Category category) {
        restTemplate.put(categoruUrl + "/category", category);
        return true;
    }

    @Override
    public boolean insert(Category category) {
        restTemplate.delete(categoruUrl + "/category", category);
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        return false;
    }
}
